function request(method, url, data, resType) {
  return new Promise((resolve, reject) => {
    try {
      let xhr = new XMLHttpRequest, params = [];
      xhr.responseType = resType || 'json';// 默认返回类型为json

      xhr.onload = () => {
          switch (xhr.status) {
              case 200: resolve(xhr.response);break;
              case 204: resolve();break;
              case 401:// 401登录失败处理
                  //do something
                  reject();
                  break;
              default: reject({status: xhr.status, res: xhr.response});
          }
      };

      xhr.onerror = () => {
        reject(new Error(xhr.statusText))
      }
        
      if (data) {// 将参数编码成表单的键值对形式
          for (let k in data) params.push(`${encodeURIComponent(k)}=${encodeURIComponent(data[k])}`);
          data = params.join('&');
      }
      xhr.open(method, url, true);
      xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded;charset=utf-8');// 设置content-type
      xhr.withCredentials = false;// 不支持跨域发送cookies
      xhr.send(data);
    }catch(err){
      reject(err)
    }
  });
}